//
//  HomeFlowViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/13.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "HZLocation.h"
#import "TT_UserInfoCollectManager.h"

#import "ActStatusRequest.h"
#import "UserInfoRequest.h"
#import "HomeADRequest.h"
#import "HomeSegRequest.h"
#import "RequestAgentInfo.h"
#import "IMAlertKeyWordRequest.h"

#import "BA_HomeNaviView.h"
#import "HomeCollectionView.h"
#import "UserNewView.h"

#import "AnimRefreshHeader.h"

#import "HomeQRCodeViewController.h"
#import "MessageCenterViewController.h"
#import "HomeCityPickerViewController.h"
#import "PlatformEquityViewController.h"
#import "UserInfoViewController.h"

#import "AgentReviewViewController.h"
#import "AgentDetailViewController.h"
#import "AgentIntroduceViewController.h"
#import "GuideUserInfoViewController.h"
#import "JLActDetailViewController.h"

#import "HomeFlowViewController.h"
#import "BusinessInfoCollectViewController.h"
#import "SelPayMethodViewController.h"
#import "InputPayViewController.h"
#import "ProClassificationModel.h"
#import "StoreDetailViewController.h"
@interface HomeFlowViewController ()<CityPickerDelegate, HZLocationDelegate>

@property (nonatomic, strong) HZLocation *locationManager;

@property (nonatomic, strong) HomeModel *homeModel;

@property (nonatomic, strong) UIButton *topBtn;

@property (nonatomic, strong) UserNewView *JLView;
@property (nonatomic, strong) BA_HomeNaviView *naviView;
@property (nonatomic , strong) HomeCollectionView *homeCollectionView;
@property (nonatomic, strong) HomeCityPickerViewController *cityPickerVC;

@end

@implementation HomeFlowViewController
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(logoutSuccessAction) name:@"BA_LogOutSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginSuccessAction) name:@"BA_LoginSuccess" object:nil];
    [self initView];
    [self initViewAction];
    [self checkLocal];
    if ([TTUserModel sharedObject].token.length > 0) {
        //登录的时候要弹弹窗
        [self loadAlert];
    }
    /**
     SelPayMethodViewController *vc = [[SelPayMethodViewController alloc] init];
     [self.navigationController pushViewController:vc animated:YES];
     
     InputPayViewController *vc = [[InputPayViewController alloc] init];
     vc.dataJson = @{
         @"sellerId" : @"1",
         @"type" : @"LKL"
         //@"type" : @"UNION"
     };
     [self.navigationController pushViewController:vc animated:YES];
     */
    //StoreDetailViewController *vc = [[StoreDetailViewController alloc] init];
    //vc.storeId = @"8543185832787509248";
    //vc.storeId = @"8544137255096352768";
    //[self.navigationController pushViewController:vc animated:YES];
}

- (void)loadAlert {
    NSTimeInterval marryDayLastTime = [[Utilities getFromNSUserDefaults:@"BA_homeMarryDayAlert"] doubleValue];

    //这里是婚期弹窗
    if (![Utilities isSameDay:[Utilities getCurrentTimestamp] Time2:marryDayLastTime]) {
        [[BA_BusinessHelper shared] callMarryCountDownViewWithType:MarryCountPlaceType_home];
        [BA_BusinessHelper shared].BusinessHelperDismissBlock = ^(BOOL success) {
            if (success) {
                //更新用户信息到usermodel
                [UserInfoRequest request_userInfoComption:^(BOOL success, UserInfoModel * _Nonnull model) {}];
            }
        };
    } else {}
}

- (void)initView {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = UIColorHex(0xf1f1f1);
    //屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
#if RT_INTERACTIVE_PUSH
    self.rt_navigationController.rt_enableInteractivePush = YES;
#endif
    
    self.topBtn.hidden = YES;
    
    self.naviView = [[NSBundle mainBundle] loadNibNamed:@"BA_HomeNaviView"owner:nil options:nil].firstObject;
    [self.view addSubview:self.naviView];
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(kStatusBarHeight + NavBar_Height);
    }];
}

- (void)initConfigView {
    
    [self.view addSubview:self.homeCollectionView];
    
    [self.view addSubview:self.topBtn];
    [self.topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-6);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.homeCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.mas_equalTo(kStatusBarHeight + NavBar_Height);
    }];
    
    [self.view addSubview:self.JLView];
    self.JLView.hidden = YES;
    [self.JLView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(-100);
        make.width.height.mas_equalTo(100);
    }];
    
    //这些计算需要在网络请求后和reloaddata前刷新
    self.homeModel.recommandHeight = TTScreenHeight - TabBar_Height - (kStatusBarHeight + NavBar_Height);
    [self addChildViewController:self.homeCollectionView.pageViewController];
    
    [RACObserve(self.homeCollectionView.collectionView,contentOffset) subscribeNext:^(id x) {
        if (self.homeCollectionView.collectionView.contentOffset.y>(TTScreenHeight - 200)) {
            self.topBtn.hidden = NO;
        }else{
            self.topBtn.hidden = YES;
        }
        CGFloat height = 200;
        CGFloat alpha = self.homeCollectionView.collectionView.contentOffset.y/height>1?1:self.homeCollectionView.collectionView.contentOffset.y/height;
        [self.naviView fillWithAlpha:alpha];
    }];
}
#pragma mark - 网络请求
- (void)request_data {
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在请求数据"];
    co_launch(^{
        await([self co_request_homeMain]);
        await([self co_request_catList]);
        NSError *error = co_getError();
        [SVProgressHUD dismiss];
        
        if (error) {
        } else {
            [self.homeCollectionView.collectionView.mj_header endRefreshing];
            [self.homeCollectionView.collectionView.mj_footer endRefreshing];
            [self initConfigView];
            [self.naviView fillWithModel:self.homeModel.head];
            [self.homeCollectionView fillWithModel:self.homeModel];
            
            [[BA_BusinessHelper shared] firstInstallPush];
            
            [ActStatusRequest request_ActStatusComption:^(BOOL success, ActStatusModel * _Nonnull model) {
                if (success && model.isDisabled == 1) {weakSelf.JLView.hidden = NO;}
                else {weakSelf.JLView.hidden = YES;}}];
            
            if ([TTUserModel sharedObject].token.length > 0) {
                [IMAlertKeyWordRequest request_imKeyWordComption:^(BOOL success) {}];
            }
        }
    });
}

- (void)goTop {
    self.homeCollectionView.canScroll = YES;
    [self.homeCollectionView subTabViewCanScroll:NO];
    [self.homeCollectionView.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}

//防止需要处理啥东西，所以在此拦截下
- (void)jumpWithModel:(HomeADItemEventModel *)jumpModel {
    if (jumpModel.eventClickId == TT_JUMPTYPE_H5 || jumpModel.eventClickId == TT_JUMPTYPE_MarryTool || jumpModel.eventClickId == TT_JUMPTYPE_JL_ACT) {
        [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
            if (isLogin) {
                [[TT_JumpManager sharedManager] jumpWithModel:jumpModel completion:^(BOOL success, NSString * _Nonnull message) {
                    
                }];
            }
        }];
    } else {
        [[TT_JumpManager sharedManager] jumpWithModel:jumpModel completion:^(BOOL success, NSString * _Nonnull message) {
            
        }];
    }
}

- (void)goMsg {
    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
        if (isLogin) {
            MessageCenterViewController *vc = [[MessageCenterViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
}

- (void)goQRCode {
    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
        if (isLogin) {
            HomeQRCodeViewController *vc = [[HomeQRCodeViewController alloc] init];
            [self QRCodeScanVC:vc];
        }
    }];
}

- (void)QRCodeScanVC:(UIViewController *)scanVC {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (device) {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        switch (status) {
                case AVAuthorizationStatusNotDetermined: {
                    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                        if (granted) {
                            dispatch_sync(dispatch_get_main_queue(), ^{
                                [self.navigationController pushViewController:scanVC animated:YES];
                            });
                            NSLog(@"用户第一次同意了访问相机权限 - - %@", [NSThread currentThread]);
                        } else {
                            NSLog(@"用户第一次拒绝了访问相机权限 - - %@", [NSThread currentThread]);
                        }
                    }];
                    break;
                }
                case AVAuthorizationStatusAuthorized: {
                    [self.navigationController pushViewController:scanVC animated:YES];
                    break;
                }
                case AVAuthorizationStatusDenied: {
                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"扫码功能需要您打开相机权限\n[设置 - 隐私 - 相机 - 婚前婚后]\n打开相机权限开关即可使用" preferredStyle:(UIAlertControllerStyleAlert)];
                    UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    
                    [alertC addAction:alertA];
                    [self presentViewController:alertC animated:YES completion:nil];
                    break;
                }
                case AVAuthorizationStatusRestricted: {
                    NSLog(@"因为系统原因, 无法访问相册");
                    break;
                }
                
            default:
                break;
        }
        return;
    }
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"未检测到您的摄像头" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertC addAction:alertA];
    [self presentViewController:alertC animated:YES completion:nil];
}

- (void)initViewAction {
    WEAKSELF
    
    self.JLView.UserNewViewBlock = ^(UserNewViewActionType type) {
        
        switch (type) {
            case UserNewViewActionType_close:{
                weakSelf.JLView.hidden = YES;
            } break;
            case UserNewViewActionType_handle:{
                
                [weakSelf openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                    if (isLogin) {
                        HomeADItemEventModel *actionModel = [[HomeADItemEventModel alloc] init];
                        actionModel.eventClickId = TT_JUMPTYPE_JL_ACT;
                        actionModel.eventSellerId = @"";
                        [[TT_JumpManager sharedManager] jumpWithModel:actionModel completion:^(BOOL success, NSString * _Nonnull message) {}];
                    }
                }];
            } break;
            default: break;
        }
    };

    
    self.naviView.BA_HomeNaviViewBlock = ^(HomeNaviActionType type, NSInteger index) {
        switch (type) {
            case HomeNaviActionType_changeCity:{
                weakSelf.cityPickerVC.modalPresentationStyle = UIModalPresentationFullScreen;
                [weakSelf presentViewController:weakSelf.cityPickerVC animated:YES completion:nil];
            } break;
            case HomeNaviActionType_search:{
                HomeADItemEventModel *model = [[HomeADItemEventModel alloc] init];
                model.eventClickId = TT_JUMPTYPE_Search;
                model.iconName = @"";
                [weakSelf jumpWithModel:model];
            } break;
            case HomeNaviActionType_message:{
                [weakSelf goMsg];
            } break;
            case HomeNaviActionType_qrCode:{
                [weakSelf goQRCode];
            } break;
            default:
                break;
        }
    };
    
    self.homeCollectionView.HomeCollectionViewBlock = ^(HomeCollectionViewActionType type, HomeADItemEventModel * _Nonnull jumpModel) {
        switch (type) {
            case HomeCollectionViewActionType_AD:{
                [weakSelf jumpWithModel:jumpModel];
            } break;
            case HomeCollectionViewActionType_Recommner:{

                [RequestAgentInfo request_agentInfoComption:^(BOOL success, AgentReviewModel * _Nonnull model) {
                   
                    if (success) {
                        
                        switch (model.type) {
                            case AgentApplyStatusType_agent:{
                                
                                switch (model.examState) {
                                    case AgentReviewStatusType_review:{
                                        AgentReviewViewController *vc = [[AgentReviewViewController alloc] init];
                                        vc.model = model;
                                        [weakSelf.navigationController pushViewController:vc animated:YES];
                                    } break;
                                    case AgentReviewStatusType_passed:{
                                        AgentDetailViewController *vc = [[AgentDetailViewController alloc] init];
                                        [weakSelf.navigationController pushViewController:vc animated:YES];
                                    } break;
                                    case AgentReviewStatusType_refuse:{
                                        AgentReviewViewController *vc = [[AgentReviewViewController alloc] init];
                                        vc.model = model;
                                        [weakSelf.navigationController pushViewController:vc animated:YES];
                                    } break;
                                    default:
                                        break;
                                }

                            } break;
                            case AgentApplyStatusType_goldAgent:{

                                AgentIntroduceViewController *vc = [[AgentIntroduceViewController alloc] init];
                                [weakSelf.navigationController pushViewController:vc animated:YES];
                                
                            } break;
                            case AgentApplyStatusType_all:{
                                switch (model.examState) {
                                    case AgentReviewStatusType_review:{
                                        AgentReviewViewController *vc = [[AgentReviewViewController alloc] init];
                                        vc.model = model;
                                        [weakSelf.navigationController pushViewController:vc animated:YES];
                                    } break;
                                    case AgentReviewStatusType_passed:{
                                        AgentDetailViewController *vc = [[AgentDetailViewController alloc] init];
                                        [weakSelf.navigationController pushViewController:vc animated:YES];
                                    } break;
                                    case AgentReviewStatusType_refuse:{
                                        AgentReviewViewController *vc = [[AgentReviewViewController alloc] init];
                                        vc.model = model;
                                        [weakSelf.navigationController pushViewController:vc animated:YES];
                                    } break;
                                    default:
                                        break;
                                }
                            } break;
                            case AgentApplyStatusType_none:{
                                AgentIntroduceViewController *vc = [[AgentIntroduceViewController alloc] init];
                                [weakSelf.navigationController pushViewController:vc animated:YES];
                            } break;
                            default:
                                break;
                        }
                    } else {
                        AgentIntroduceViewController *vc = [[AgentIntroduceViewController alloc] init];
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }
                }];
            } break;
            case HomeCollectionViewActionType_NewFree:{
            } break;
            case HomeCollectionViewActionType_PlatformEquity:{
                PlatformEquityViewController *vc = [[PlatformEquityViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
                
            default:
                break;
        }
    };
}
#pragma mark - 定位相关以及代理
- (void)checkLocal {
    //如果已选择，那就加载已选城市，如果没选择就去定位
    if ([TTUserModel sharedObject].cityName.length > 0) {
        self.naviView.cityLab.text = [TTUserModel sharedObject].cityName;
        [self request_data];
    } else {
        self.locationManager.delegate = self;
    }
}

- (void)locating {}

//定位成功
- (void)currentLocation:(NSDictionary *)locationDictionary coordinate:(CLLocationCoordinate2D)coordinate {
    NSString *city = [locationDictionary valueForKey:@"City"];
    [[TTUserModel sharedObject].userModelDefault setDouble:coordinate.latitude forKey:@"TT_latitude"];
    [[TTUserModel sharedObject].userModelDefault setDouble:coordinate.longitude forKey:@"TT_longitude"];
    [self request_cityIdWith:city];
}

/// 拒绝定位
- (void)refuseToUsePositioningSystem:(NSString *)message {
    [SVProgressHUD showWithStatus:@"app未获取到定位权限，如果使用定位功能，请同意定位权限，或者在‘设置’-‘隐私’-‘定位服务’中找到app，并且开启定位权限"];
    [SVProgressHUD dismissWithDelay:2];
}

/// 定位失败
- (void)locateFailure:(NSString *)message {
    [TT_Alert alert:@"定位失败，无法获取当前城市，您可以手动选择为您服务的城市" cancleBtnTitle:@"默认城市" cancleAction:^{
        [self request_cityIdWith:@"合肥市"];
    } doneBtnTitle:@"选择城市" doneAction:^{
        self.cityPickerVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:self.cityPickerVC animated:YES completion:nil];
    }];
}

#pragma mark - CityPickerDelegate
- (void) cityPickerController:(CityPickerController *)cityPickerViewController didSelectCity:(CityModel *)city {
    self.naviView.cityLab.text = city.cityName;
    [[TTUserModel sharedObject].userModelDefault setValue:city.cityName forKey:@"TT_cityName"];
    [[TTUserModel sharedObject].userModelDefault setValue:city.cityID forKey:@"TT_cityId"];
    [[TTUserModel sharedObject].userModelDefault synchronize];
    [self request_data];
    [self.cityPickerVC dismissViewControllerAnimated:YES completion:nil];
}

- (void)cityPickerControllerDidCancel:(CityPickerController *)cityPickerViewController {
    [self.cityPickerVC dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark- 懒加载
- (HomeCollectionView *)homeCollectionView {
    if (!_homeCollectionView) {
        _homeCollectionView = [[HomeCollectionView alloc] init];
        WEAKSELF
        AnimRefreshHeader *refreshHeader = [AnimRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf request_data];
        }];
        [_homeCollectionView.collectionView setMj_header:refreshHeader];
    }
    return _homeCollectionView;
}

- (HomeModel *)homeModel {
    if (!_homeModel) {
        _homeModel = [[HomeModel alloc] init];
    }
    return _homeModel;
}

- (HomeCityPickerViewController *)cityPickerVC {
    if (!_cityPickerVC) {
        _cityPickerVC = [[HomeCityPickerViewController alloc] init];
        [_cityPickerVC.cityPickerVC setDelegate:self];
    }
    return _cityPickerVC;
}

- (HZLocation *)locationManager {
    if (!_locationManager) {
        _locationManager = [[HZLocation alloc] init];
    }
    return _locationManager;
}

- (UIButton *)topBtn {
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topBtn setImage:[UIImage imageNamed:@"to_top"] forState:UIControlStateNormal];
        [_topBtn addTarget:self action:@selector(goTop) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topBtn;
}

- (UserNewView *)JLView {
    if (!_JLView) {
        _JLView =  [[NSBundle mainBundle] loadNibNamed:@"UserNewView"owner:nil options:nil].firstObject;
    }
    return _JLView;
}

- (void)handleCustomAttribute:(NSDictionary *)userInfo {
    if ([userInfo.allKeys containsObject:@"customAttribute"]) {
        //后台推送
        NSString *customAttribute = userInfo[@"customAttribute"];
        NSData *data = [customAttribute dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",json);
        //HomeADItemEventModel
        HomeADItemEventModel *eventModel = [HomeADItemEventModel modelWithJSON:json];
        [[TT_JumpManager sharedManager]jumpWithModel:eventModel completion:^(BOOL success, NSString * _Nonnull message) {
            
        }];
    }
}

- (COPromise<id> *)co_request_homeMain {
    WEAKSELF
    return [COPromise promise:^(COPromiseFulfill  _Nonnull fullfill, COPromiseReject  _Nonnull reject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [HomeADRequest request_homeMainWithCityName:[TTUserModel sharedObject].cityName cityId:[TTUserModel sharedObject].cityId comption:^(BOOL success, HomeModel * _Nonnull homeModel) {
                if (success) {
                    weakSelf.homeModel = homeModel;
                    fullfill(homeModel);
                } else {
                    weakSelf.homeModel = homeModel;
                    reject([NSError new]);
                }
            }];
        });
    }];
}

- (COPromise<id> *)co_request_catList {
    WEAKSELF
    return [COPromise promise:^(COPromiseFulfill  _Nonnull fullfill, COPromiseReject  _Nonnull reject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [HomeSegRequest request_homeSegWithParentId:@"-1" scType:@"1" comption:^(BOOL success, NSMutableArray<HomeFlowSegmentModel *> * _Nonnull segDataArray) {
                if (success) {
                    weakSelf.homeModel.segDataArray = segDataArray.mutableCopy;
                    fullfill(weakSelf.homeModel);
                } else {
                    reject([NSError new]);
                }
            }];
        });
    }];
}

#pragma mark 登录成功的处理
- (void)loginSuccessAction {
    //登录后先采集信息、再弹窗
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //收集数据，完成了给回调
        [TT_UserInfoCollectManager collectUserInfoWithType:UserInfoCollectType_UnFinishProj Comption:^(BOOL isClose, BOOL isFinishCollect, NSString *resultStr) {
            if (isFinishCollect) {
                if (resultStr.length > 0) {
                    [[TT_IMContactManager manager] ImContactWithType:TT_IMContactType_tongchoushi withQuickReply:resultStr];
                }
            }
            if (isClose) {
                [self loadAlert];
                
                //收集信息弹窗消失后
                if ([TTUserModel sharedObject].isRegister) {
                    //引导用户填写信息弹窗
                    TT_NavigationViewController *navController = (TT_NavigationViewController *)[TT_TabbarViewController shareTabbar].selectedViewController;
                    GuideUserInfoViewController *vc = [[GuideUserInfoViewController alloc] init];
                    vc.completion = ^(BOOL isClose) {
                        if (isClose) {
                            [TTUserModel sharedObject].isRegister = NO;
                        }
                    };
                    [navController pushViewController:vc animated:YES];
                }
            }
        }];
    });
    
    if ([TTUserModel sharedObject].identifier.length > 0 && [TTUserModel sharedObject].userSig > 0) {
        [[TT_IMManager sharedInstance] BA_IMLogin];
    }
}

- (void)logoutSuccessAction {
    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {}];
}

- (void)request_cityIdWith:(NSString *)cityName {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:cityName forKey:@"cityName"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/indexApp/queryCityId"];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {//匹配到城市id
                NSDictionary *resultDic = [data valueForKey:@"data"];
                NSString *cityID = [resultDic valueForKey:@"sellerCityId"];
                weakSelf.naviView.cityLab.text = cityName;
                [[TTUserModel sharedObject].userModelDefault setValue:cityName forKey:@"TT_cityName"];
                [[TTUserModel sharedObject].userModelDefault setValue:cityID forKey:@"TT_cityId"];
                [[TTUserModel sharedObject].userModelDefault synchronize];
                
                [weakSelf request_data];
                
            } else {//未匹配到城市id
                [TT_Alert alert:[data valueForKey:@"message"] cancleBtnTitle:@"再看看" cancleAction:^{
                } doneBtnTitle:@"手动选择" doneAction:^{
                    weakSelf.cityPickerVC.modalPresentationStyle = UIModalPresentationFullScreen;
                    [weakSelf presentViewController:weakSelf.cityPickerVC animated:YES completion:nil];
                }];
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {
    }];
    
}

@end
